package demo.practice.leetcode_eveyday;

public class P80 {


    public static void main(String[] args) {
        P80 p80= new P80();
        p80.removeDuplicates(new int[]{ 0,0,1,1,1,1,2,3,3});
    }
    public int removeDuplicates(int[] nums) {

        if(nums.length<3)
            return nums.length;
        int offset=0;   //偏移
        int pre= Integer.MIN_VALUE;
        int repeat=1;   //重复次数
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]==pre){
                repeat++;
                if(repeat>2){
                    offset+=1;
                }else{
                    nums[i-offset]=nums[i];
                }
            }else{
                pre=nums[i];
                nums[i-offset]=nums[i];
                repeat=1;
            }
        }
        return nums.length-offset;
    }
}
